package com.wuliu.common.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 延迟队列业务枚举
 *
 * @author realue
 * @version 1.0
 * @date 2023-11-17 10:07
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum RedisDelayQueueEnum {
    ORDER_RECEIVE_TIMEOUT("ORDER_RECEIVE_TIMEOUT", "订单超时未接单,通知站点负责人",
            "orderReceiveTimeout", 20, TimeUnit.SECONDS),
    ORDER_COLLECT_TIMEOUT("ORDER_COLLECT_TIMEOUT", "接单超时未揽收,通知业务员",
            "orderCollectTimeout", 30, TimeUnit.MINUTES),
    ;
    /**
     * 延迟队列 Redis Key
     */
    private String queueName;

    /**
     * 中文描述
     */
    private String name;

    /**
     * 延迟队列具体业务实现的 Bean
     * 可通过 Spring 的上下文获取
     */
    private String beanId;

    /**
     * 延时时长
     */
    private long delay;

    /**
     * 时长单位
     */
    private TimeUnit timeUnit;

    /**
     * 获取延时时长
     *
     * @param name
     * @return
     */
    public static long getDelayByName(String name) {
        if (Objects.nonNull(name)) {
            for (RedisDelayQueueEnum delayQueueEnum : values()) {
                if (delayQueueEnum.getQueueName().equals(name)) {
                    return delayQueueEnum.getDelay();
                }
            }
        }
        return 0;
    }

    /**
     * 获取延时时长单位
     *
     * @param name
     * @return
     */
    public static TimeUnit getTimeUnitByName(String name) {
        if (Objects.nonNull(name)) {
            for (RedisDelayQueueEnum delayQueueEnum : values()) {
                if (delayQueueEnum.getQueueName().equals(name)) {
                    return delayQueueEnum.getTimeUnit();
                }
            }
        }
        return null;
    }
}
